package edu.ustb.mis.dm.util;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import edu.ustb.mis.dm.Constants;

public class FileUtils {
	public static String dirBuilder(final String data_set_name) {
		String index = new StringBuffer(Constants.OUT_PREFIX).append(File.separator).append(Constants.RESULT_INDEX).append(data_set_name).append(File.separator).toString();
		File file = new File(index);
		if (!file.exists()) {
			file.mkdirs();
		} else {
			final String[] fileNames = file.list();
			if (fileNames.length == 0) {
				index = new StringBuffer(index).append("1").append(File.separator).toString();
				file = new File(index);
				file.mkdirs();
			} else {
				final List<String> fileNameList = Arrays.asList(fileNames);
				Collections.sort(fileNameList, new Comparator<String>() {
					public int compare(final String o1, final String o2) {
						return Double.compare(Double.valueOf(o2), Double.valueOf(o1));
					}
				});
				index = new StringBuffer(index).append(Integer.valueOf(fileNameList.get(0)) + 1).append(File.separator).toString();
				file = new File(index);
				file.mkdirs();
			}
		}
		return index;
	}

	public static String dirBuilder(String dir, final double threshhold) {
		dir = new StringBuffer(dir).append(Constants.THRESHHOLD_FORMAT.format(threshhold)).append(File.separator).toString();
		final File file = new File(dir);
		if (!file.exists()) {
			file.mkdirs();
		}
		return dir;
	}
}
